package model.components;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * SubstrateLink Class. Subclass of Link.
 */
public class SubstrateLink extends Link {
	
	private List<LinkMapping> virtualLinks; 
	
	public SubstrateLink(int id, int bandwidth) {
		super(id, bandwidth);
		name = "substrateLink"+id;
		virtualLinks = new CopyOnWriteArrayList<LinkMapping>();
	}

	public List<LinkMapping> getVirtualLinks() {
		return virtualLinks;
	}
	
	public void setVirtualLinks(List<LinkMapping> virtualLinks) {
		this.virtualLinks = virtualLinks;
	}

	public void addVirtualLink(LinkMapping virtualLink) {
		this.virtualLinks.add(virtualLink);
	}
	public void removeVirtualLink(String reqId, int linkId) {
		for (LinkMapping lm : virtualLinks)
			if (lm.getRequest().getId().equals(reqId)
					&& lm.getRequestLink().getId() == linkId)
				virtualLinks.remove(lm);
	}

	public int getAvailableBandwidth() {
		int returnValue = this.getBandwidth();
		for (LinkMapping lm : virtualLinks)
			returnValue-=lm.getRequestLink().getBandwidth();
		return returnValue;
	}
	
	public Object getCopy() {
		SubstrateLink l = new SubstrateLink(this.getId(),this.getBandwidth());
		l.name = this.name;
		l.delay = this.delay;
		for (LinkMapping lm : this.virtualLinks) {
			l.virtualLinks.add(lm.getCopy());
		}
		return l;
	}
	
}
